﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace RegexTester
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitListView();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.tbRegex.Text))
            {
                MessageBox.Show("请输入正则表达式");
                return;
            }

            MatchText();
        }

        private void InitListView()
        {
            this.lvResult.View = View.Details;
            this.lvResult.BeginUpdate();
            this.lvResult.Columns.Add("匹配文本",500);
            this.lvResult.EndUpdate();
        }

        private void MatchText()
        {
            Regex regex;
            MatchCollection mc;
            int ColumCount = 0;

            regex = new Regex(@"\?<[^<>]*>");

            try
            {
                mc = regex.Matches(this.tbRegex.Text);
                ColumCount = mc.Count;
                this.lvResult.BeginUpdate();
                this.lvResult.Clear();
                if (ColumCount == 0)
                {
                    this.lvResult.Columns.Add("匹配文本", 600, HorizontalAlignment.Center);
                }
                else
                {
                    this.lvResult.Columns.Add("匹配文本", 200, HorizontalAlignment.Center);
                }
                for (int i = 0; mc.Count > 0 && i < mc.Count; i++)
                {
                    this.lvResult.Columns.Add(mc[i].Value.Replace(@"?<", "").Replace(@">", ""), 200, HorizontalAlignment.Center);
                }
                this.lvResult.EndUpdate();

                regex = new Regex(this.tbRegex.Text);
                mc = regex.Matches(this.rtbText.Text);
                this.groupBox2.Text = "测试结果：" + mc.Count;
                if (mc.Count > 0) this.lvResult.GridLines = true;
                foreach (Match mt in mc)
                {
                    ListViewItem newItem = new ListViewItem();
                    newItem.SubItems[0].Text = mt.Value;
                    for (int i = 1; i < this.lvResult.Columns.Count; i++)
                    {
                        newItem.SubItems.Add(mt.Groups[this.lvResult.Columns[i].Text].Value);
                    }
                    this.lvResult.Items.Add(newItem);
                }

                for (int i = 0; i < lvResult.Columns.Count; i++)
                {
                    if (this.lvResult.Columns.Count == 1) this.lvResult.Columns[0].Width = -1;
                    this.lvResult.Columns[i].Width = -1;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
